package com.tubang.logistics.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tubang.common.util.CollectionUtils;
import com.tubang.logistics.eum.OrderStatusEnum;
import com.tubang.logistics.mapper.LogisticsOrderMapper;
import com.tubang.logistics.service.IStatisticsGoodsService;
import com.tubang.logistics.util.ExcelUtils;
import com.tubang.logistics.vo.statistics.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Order;
import java.math.BigDecimal;
import java.util.*;

@Service
public class StatisticsGoodsServiceImpl implements IStatisticsGoodsService {

	@Resource
	private LogisticsOrderMapper logisticsOrderMapper;

	@Override
	public Map<String, OrderDataSimpleVO> getGoodsData(Long startTime, Long endTime, String userId) {
		List<Integer> notDoneOrderStatus = new ArrayList<>();
		notDoneOrderStatus.add(OrderStatusEnum.GRAB_ORDER.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.ASSIGNED_ORDER.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.BIDDING.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.QUOTED.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.GET_WAIT.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.TAKE_WAIT.getCode());
		notDoneOrderStatus.add(OrderStatusEnum.CHECK_WAIT.getCode());

		Map<String, OrderDataSimpleVO> result = new HashMap<>();

		Date startDate = new Date(startTime);
		Date endDate = new Date(endTime);
		List<OrderDataSimpleVO> orderDataSimpleVOS = logisticsOrderMapper.getOrderDataByUserId(startDate, endDate, userId);
		orderDataSimpleVOS.forEach(orderDataSimpleVO -> {
			if (OrderStatusEnum.FINISH.getCode().equals(orderDataSimpleVO.getOrderStatus())
					|| OrderStatusEnum.REVIEW_WAIT.getCode().equals(orderDataSimpleVO.getOrderStatus()))
				result.merge(OrderStatusEnum.FINISH.getCode().toString(), orderDataSimpleVO, (oldValue, newValue) -> {
					oldValue.setOrderNum(oldValue.getOrderNum() + newValue.getOrderNum());
					oldValue.setOrderWeight(oldValue.getOrderWeight().add(newValue.getOrderWeight()));
					return oldValue;
				});
			else if (notDoneOrderStatus.contains(orderDataSimpleVO.getOrderStatus()))
				result.merge("not_start", orderDataSimpleVO, (oldValue, newValue) -> {
					oldValue.setOrderNum(oldValue.getOrderNum() + newValue.getOrderNum());
					oldValue.setOrderWeight(oldValue.getOrderWeight().add(newValue.getOrderWeight()));
					return oldValue;
				});
			else if (OrderStatusEnum.DELIVERY.getCode().equals(orderDataSimpleVO.getOrderStatus()))
				result.merge("delivery", orderDataSimpleVO, (oldValue, newValue) -> {
					oldValue.setOrderNum(oldValue.getOrderNum() + newValue.getOrderNum());
					oldValue.setOrderWeight(oldValue.getOrderWeight().add(newValue.getOrderWeight()));
					return oldValue;
				});
			orderDataSimpleVO.setOrderStatusName(OrderStatusEnum.getVal(orderDataSimpleVO.getOrderStatus()).getText());

		});

		return result;
	}

	@Override
	public Map<String, Object> getGoodsPercent(Long startTime, Long endTime, String userId) {
		Map<String, Object> result = new HashMap<>();

		Date startDate = new Date(startTime);
		Date endDate = new Date(endTime);
		List<GoodsPercentVO> goodsPercentVOS = logisticsOrderMapper.getGoodsDataByCarrierLimitNine(startDate, endDate, userId);

		//所有订单数量和金额
		OrderDataSimpleVO orderDataSimpleVO = logisticsOrderMapper.countGoodsNumByCarrier(startDate, endDate, userId, true);
		BigDecimal otherMoney = orderDataSimpleVO.getOrderMoney();
		//商品有几种
		Long goodsTypeNum = logisticsOrderMapper.countGoodsTypeNumByCarrier(startDate, endDate, userId);
		//成品有多少
		Long productNum = logisticsOrderMapper.countGoodsNumByGoodsNameAndCarrier(startDate, endDate, "成品", userId);
		//原材料有多少
		Long materials = logisticsOrderMapper.countGoodsNumByGoodsNameAndCarrier(startDate, endDate, "原材料", userId);

		BigDecimal otherPercent = new BigDecimal(100);
		Integer otherNum = orderDataSimpleVO.getOrderNum();
		for (GoodsPercentVO goodsPercentVO : goodsPercentVOS) {
			BigDecimal percent = new BigDecimal(goodsPercentVO.getOrderNum()).divide(
					new BigDecimal(orderDataSimpleVO.getOrderNum()), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100));
			otherPercent = otherPercent.subtract(percent);
			otherNum = otherNum - goodsPercentVO.getOrderNum();
			goodsPercentVO.setPercent(percent.intValue());
		}

		if (!CollectionUtils.isEmpty(goodsPercentVOS)) {
			GoodsPercentVO goodsPercentVO = new GoodsPercentVO();
			goodsPercentVO.setGoodsName("其他");
			goodsPercentVO.setPercent(otherPercent.intValue());
			goodsPercentVO.setOrderNum(otherNum);
			goodsPercentVO.setOrderMoney(otherMoney.setScale(4, BigDecimal.ROUND_HALF_UP));
			goodsPercentVOS.add(goodsPercentVO);
		}

		result.put("percent", goodsPercentVOS);
		result.put("goodsTypeNum", goodsTypeNum);
		result.put("productNum", productNum);
		result.put("materials", materials);
		return result;
	}

	@Override
	public Map<String, Object> getGoodsTable(Long startTime, Long endTime, String userId, Boolean productCategory, String content, Integer page, Integer rows) {
		Map<String, Object> resultMap = new HashMap<>();
		Map<String, Object> paramMap = new HashMap<>();

		PageHelper.startPage(page, rows);
		paramMap.put("startTime", new Date(startTime));
		paramMap.put("endTime", new Date(endTime));
		paramMap.put("userId", userId);
		paramMap.put("product", productCategory);
		paramMap.put("content", StringUtils.isNotBlank(content) ? content : null);
		List<GoodsTableVO> goodsTableVOS = logisticsOrderMapper.getGoodsTableByParam(paramMap);
		Page pageBean = (Page) goodsTableVOS;

		GoodsDataSimpleVO goodsDataSimpleVO = logisticsOrderMapper.getGoodsSumByParam(paramMap);

		paramMap.clear();
		paramMap.put("goodsTableVOS", goodsTableVOS);
		paramMap.put("orderDataSimpleVO", goodsDataSimpleVO);
		resultMap.put("result", paramMap);
		resultMap.put("total", pageBean.getTotal());
		return resultMap;
	}

	@Override
	public HSSFWorkbook exportGoodsTable(Long startTime, Long endTime, String userId, Boolean productCategory, String content) {
		Map<String, Object> paramMap = new HashMap<>();

		paramMap.put("startTime", new Date(startTime));
		paramMap.put("endTime", new Date(endTime));
		paramMap.put("userId", userId);
		paramMap.put("product", productCategory);
		paramMap.put("content", StringUtils.isNotBlank(content) ? content : null);
		List<GoodsTableVO> goodsTableVOS = logisticsOrderMapper.getGoodsTableByParam(paramMap);

		//excel内容
		String[][] excelContent = new String[goodsTableVOS.size() + 1][7];

		//表头
		excelContent[0][0] = "货源名称";
		excelContent[0][1] = "货源类型";
		excelContent[0][2] = "共发布";
		excelContent[0][3] = "已完成";
		excelContent[0][4] = "未运输";
		excelContent[0][5] = "运输中";
		excelContent[0][6] = "异常单";


		//内容
		for (int i = 1; i < goodsTableVOS.size() + 1; i++) {
			GoodsTableVO goodsTableVO = goodsTableVOS.get(i - 1);
			excelContent[i][0] = goodsTableVO.getGoodsName();
			excelContent[i][1] = goodsTableVO.getCategory();
			excelContent[i][2] = goodsTableVO.getAllOrderNum() + "/" + goodsTableVO.getAllWeight().toPlainString();
			excelContent[i][3] = goodsTableVO.getFinishOrderNum() + "/" + goodsTableVO.getFinishWeight().toPlainString();
			excelContent[i][4] = goodsTableVO.getNotSendOrderNum() + "/" + goodsTableVO.getNotSendWeight().toPlainString();
			excelContent[i][5] = goodsTableVO.getSendOrderNum() + "/" + goodsTableVO.getSendWeight().toPlainString();
			excelContent[i][6] = goodsTableVO.getErrorOrderNum() + "/" + goodsTableVO.getErrorWeight().toPlainString();

		}

		//poi
		return ExcelUtils.createMoreSheetHSSFWorkbook(excelContent);
	}

}
